package com.zyk.leetcode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * @author zhangsan
 * @date 2021/4/15 10:44
 */
public class C146 {

    static public class LRUCache {

        Queue<Integer> keys = new LinkedList<>();
        Map<Integer, Integer> cache = new HashMap<>();
        int capacity;

        public LRUCache(int capacity) {
            this.capacity = capacity;
        }

        public int get(int key) {
            if(keys.contains(key)) {
                keys.remove(key);
                keys.add(key);
                return cache.get(key);
            }
            return -1;
        }

        public void put(int key, int value) {
            if( keys.contains(key) ) {
                keys.remove(key);
            } else if( keys.size() == capacity ) {
                Integer delKey = keys.poll();
                cache.remove(delKey);
            }
            keys.add(key);
            cache.put(key, value);
        }

    }

}
